

function addcount() {
	usrinfo = {
		name: "13148458056",
		pwd: "0D6B3B856DBA07F79BCE1496D8C643C4",
		token: null
	};

	var users = JSON.parse(localStorage.getItem('$wituser') || '[]');
	for(var index = 0; index < users.length; index++) {
		if(users[index].name == usrinfo.name) {
			plus.nativeUI.toast("已经存在！");
			return;
		}
	}

	users.push(usrinfo);
	localStorage.setItem('$wituser', JSON.stringify(users)); 
}
 
 function getAccount(){
 	var users = JSON.parse(localStorage.getItem('$wituser') || '[]');
 	return users;
 }
 
function login(callback) {
	var users = JSON.parse(localStorage.getItem('$wituser') || '[]');
	if(users.length <= 0) {
		return;
	}
	
	// 判断是否已经登入
	var user = users[0];
	if(user.token != ""){
		// 查询订单
		if(isOnLine(user.token)){
			callback(user.token);
			return;
		}
	}
	// 重新登入
	var parms = {
		clientVersion: "1.1.9",
		deviceImei: "6e4f00d815d594f593b82d55c365fa413da56152",
		deviceModel: "iPhone",
		deviceVersion: "10.3.2",
		loginName: user.name,
		operator: "中国联通",
		password: user.pwd,
		regSource: "20000",
		systemName: "2",
		version: "1.1.9"
	};
	$.post("http://stock.oksdb.com/user/login", parms, function(data) {
		var jsondata = JSON.parse(data);
		var token = jsondata["data"]["tokenInfo"]["token"];
		users[0].token = token;
		localStorage.removeItem("$wituser");
		localStorage.setItem('$wituser', JSON.stringify(users));

		callback(token);
	});
}
//获取订单
function getOrders(tokenid){
	var params = {
		fundType:"1",
		futuresType:"1002",
        token:tokenid
	};
	$.ajaxSetup({   
            async : false  
        });
    var orders=[];
	$.post("http://stock.oksdb.com/order/futures/posiList",params,function(data){
		var jsondata = JSON.parse(data);
		var lis = jsondata["data"]["futuresScoreOrderList"];
		for(var i = 0; i < lis.length;i++){
			var order = lis[i];
			orders.push(order["id"]);
		}
	});
	
	return orders;
}

// 购买,0涨，1空
function buy(tokenid,type){
	// 获取时间
    var t = getSystemTime();
    var priceinfo = queryPrice();
    var price= priceinfo["price"];
    var code = priceinfo["code"];
    payload = {
    	orderData:"{'futuresCode' : '"+code+"','rate' : 7.1,'stopProfit' : 1800,'tradeType' :"+ type+",'count' : 1,'couponIds' : '[]','traderId' : '13235','userBuyDate' : '" +t+"','userBuyPrice' : '"+price+"'}",
    	token:tokenid,
    	version:"0.0.3"
    };
    
    $.ajaxSetup({   
            async : false  
        });
    var result;
    $.post("http://stock.oksdb.com/order/futures/buy", payload,function(data){
    	alert(data);
    })
    return result;
}

// 卖出
function sell(tokenid,orderid){
	//获取时间
    var t = getSystemTime();
    var priceinfo = queryPrice();
    var price= priceinfo["price"];
    var code = priceinfo["code"];
    payload={
    	shouldCheckPrice:"true",
    	orderId:"[{'orderId':'','fundType':'0'},{'orderId':'" +orderid + "','fundType':'1'}]",
    	token:tokenid,
    	userSaleDate:t,
    	userSalePrice:price,
    	version:"0.0.3"
    }
    
    $.ajaxSetup({   
            async : false  
        }); 
    var result = "";
    $.post("http://stock.oksdb.com/order/futures/sale", payload,function(data){
    	result = data;
    })
    return result;
}

function getSystemTime(){
	var s = "";
	// 采用同步方式请求
	$.ajaxSetup({   
            async : false  
        }); 
	$.post("http://stock.oksdb.com/user/sysTime",function(data){
		var json = JSON.parse(data);
		s = json["data"]["nowTime"];
	})
	return s;
}

function isOnLine(tokenid){
	$.ajaxSetup({   
            async : false  
        });
        
        var params = {
		fundType:"1",
		futuresType:"1002",
        token:tokenid
	};
	var result = false;
	$.post("http://stock.oksdb.com/order/futures/posiList",params,function(data){
		var jsondata = JSON.parse(data);
		if(jsondata["code"] == "200")
		{
			result = true;
		}
	});
	
	return result;
}

function queryPrice(){
	$.ajaxSetup({   
            async : false  
        }); 
    var s={}
	$.post("http://stock.oksdb.com/futuresquota/getCacheData",{futuresType:"CL"},function(data){
		var json = JSON.parse(data);
		s = {
			price:json["data"]["lastPrice"],
			code:json["data"]["code"]
			};
	})
	return s;
}
